<template>
  <div class="p-4 top-box" style="position: sticky; top: 0px; z-index: 1000">
    <div class="fs-5 tieba">贴吧</div>
    <div>
      <NSpace>
        <NInput v-model:value="queryInfo.info" placeholder="标题搜索"></NInput>
        <NButton type="primary" @click="search">查询</NButton>
      </NSpace>
    </div>
    <HuUserInfo></HuUserInfo>
  </div>

  <!-- 切换排序 -->
  <div class="pl-20 pr-20" style="width: 50vw; margin: 0px auto">
    <NTabs @update-value="changeOrder">
      <NTabPane name="1" tab="最新"></NTabPane>
      <NTabPane name="2" tab="高赞"></NTabPane>
      <NTabPane name="3" tab="热评"></NTabPane>
      <NTabPane name="4" tab="热门"></NTabPane>
    </NTabs>
  </div>

  <!-- 骨架屏效果  -->
  <div v-if="loading">
    <div v-for="i in 10" :key="i" style="width: 50vw; margin: 0px auto">
      <NCard>
        <NSkeleton text></NSkeleton>
        <NSkeleton text style="height: 50px"></NSkeleton>
      </NCard>
    </div>
  </div>

  <!-- 数据列表 -->
  <div v-else style="width: 50vw; margin: 0px auto">
    <div v-for="d in list" :key="d.mid" class="mb-3">
      <NCard :title="d.user.info.nickname">
        <div>
          <div class="fs-5" @click="toDetail(d.mid)">{{ d.title }}...</div>
          <div class="detail mb-2 mt-2">
            <div>
              {{ Tools.formatDate(d.lastupdate, 'yyyy-MM-dd') }}
              点击量: {{ d.hits }} 点赞量：{{ d.praise }}
            </div>
            <div>
              <NPopover trigger="hover">
                <span>点赞量: {{ d.praise }}</span>
                <template #trigger>
                  <NIcon size="20" @click="likeMessage(d)" :class="{ active: d.exInfo.praised }">
                    <svg
                      t="1745979472189"
                      class="icon"
                      viewBox="0 0 1024 1024"
                      version="1.1"
                      xmlns="http://www.w3.org/2000/svg"
                      p-id="4953"
                      width="256"
                      height="256"
                    >
                      <path
                        d="M885.9 533.7c16.8-22.2 26.1-49.4 26.1-77.7 0-44.9-25.1-87.4-65.5-111.1a67.67 67.67 0 0 0-34.3-9.3H572.4l6-122.9c1.4-29.7-9.1-57.9-29.5-79.4-20.5-21.5-48.1-33.4-77.9-33.4-52 0-98 35-111.8 85.1l-85.9 311H144c-17.7 0-32 14.3-32 32v364c0 17.7 14.3 32 32 32h601.3c9.2 0 18.2-1.8 26.5-5.4 47.6-20.3 78.3-66.8 78.3-118.4 0-12.6-1.8-25-5.4-37 16.8-22.2 26.1-49.4 26.1-77.7 0-12.6-1.8-25-5.4-37 16.8-22.2 26.1-49.4 26.1-77.7-0.2-12.6-2-25.1-5.6-37.1zM184 852V568h81v284h-81z m636.4-353l-21.9 19 13.9 25.4c4.6 8.4 6.9 17.6 6.9 27.3 0 16.5-7.2 32.2-19.6 43l-21.9 19 13.9 25.4c4.6 8.4 6.9 17.6 6.9 27.3 0 16.5-7.2 32.2-19.6 43l-21.9 19 13.9 25.4c4.6 8.4 6.9 17.6 6.9 27.3 0 22.4-13.2 42.6-33.6 51.8H329V564.8l99.5-360.5c5.2-18.9 22.5-32.2 42.2-32.3 7.6 0 15.1 2.2 21.1 6.7 9.9 7.4 15.2 18.6 14.6 30.5l-9.6 198.4h314.4C829 418.5 840 436.9 840 456c0 16.5-7.2 32.1-19.6 43z"
                        p-id="4954"
                      ></path>
                    </svg>
                  </NIcon>
                </template>
              </NPopover>
            </div>
          </div>
        </div>
      </NCard>
    </div>
  </div>
  <div class="p-4 tc" v-if="loading">
    <span><NSpin size="small"></NSpin>正在加载中...</span>
  </div>
  <div class="p-4 tc" v-if="page.pageNumber >= page.pageCount && !loading">我是有底线的</div>
  <NBackTop></NBackTop>
</template>

<script setup lang="ts">
import HuUserInfo from '@/components/HuUserInfo.vue'
import { ref, onMounted, onUnmounted } from 'vue'
import type { TbMessage } from '@/types/Message'
import type { BaseDataResult, PageBean, PageVO } from '@/types/BaseResult'
import { Api } from '@/types/Api'
import { Tools } from '@/types/Tools'
import { useRouter } from 'vue-router'
import {
  NButton,
  NInput,
  NTabPane,
  NTabs,
  useMessage,
  NSpace,
  NCard,
  NSkeleton,
  NPopover,
  NSpin,
  NBackTop,
  NIcon,
} from 'naive-ui'

const router = useRouter()
const message = useMessage()
const loading = ref(false)

// #region 查询的部分
const page = ref({ pageNumber: 1, pageSize: 10 } as PageBean)
const list = ref([] as TbMessage[])
const queryInfo = ref({ order: 1, info: '' })

const query = () => {
  loading.value = true
  Api.get(
    `/message/${page.value.pageSize}/${page.value.pageNumber}/${queryInfo.value.order}`,
    { info: queryInfo.value.info },
    (data: BaseDataResult<PageVO<TbMessage>>) => {
      loading.value = false
      if (data.success) {
        list.value = list.value.concat(data.data.list)
        page.value = data.data.pageBean
        return
      }
      message.error(data.message)
    },
  )
}

const changeOrder = (order: number) => {
  queryInfo.value.order = order
  requery()
}

const search = () => {
  page.value.pageNumber = 1
  list.value = []
  query()
}

const requery = () => {
  page.value.pageNumber = 1
  list.value = []
  query()
}

const loadmore = () => {
  page.value.pageNumber++
  if (page.value.pageNumber >= page.value.pageCount) {
    return
  }
  query()
}
// #endregion

//#region 滚动加载
const scroll = () => {
  if (loading.value) {
    return
  }
  if (isScrollToBottom()) {
    loadmore()
  }
}

const isScrollToBottom = () => {
  const scrollTop = document.documentElement.scrollTop || window.scrollY
  const sh = document.documentElement.scrollHeight
  const ch = document.documentElement.clientHeight
  return scrollTop + ch >= sh - 100
}
//#endregion

//#region 点赞
const likeMessage = (info: TbMessage) => {
  Api.post(`/message/praise/${info.mid}`, {}, (data: BaseDataResult<number>) => {
    if (data.success) {
      info.exInfo.praised = !info.exInfo.praised
      return
    }
    message.error(data.message)
    console.log(data)
  })
}
//#endregion
//#region 详情
const toDetail = (mid: number) => {
  router.push(`/message/${mid}`)
}
//#endregion

onMounted(() => {
  requery()
  window.addEventListener('scroll', scroll)
})

onUnmounted(() => {
  window.removeEventListener('scroll', scroll)
})
</script>

<style scoped lang="scss">
.tieba {
  color: white;
}

.top-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  position: sticky;
  top: 0px;
  z-index: 10;
  background-color: var(--body-color);
}

.vimg {
  display: block;
  height: 7vw;
}

.img-box {
  display: flex;
  gap: 1rem;
  > div {
    flex: 1;
  }
  img {
    display: block;
    width: 100%;
    aspect-ratio: 16 / 9;
    object-fit: cover;
  }
}

.detail {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .active {
    color: #ff0000;
  }
}
</style>
